using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using Geekees.App.Cashbook.Lib;
using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Register.
	/// </summary>
	public class Register : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtUserName;
		protected System.Web.UI.WebControls.TextBox txtPwd;
		protected System.Web.UI.WebControls.TextBox txtConfirmPwd;
		protected System.Web.UI.WebControls.Button btnRegister;
		protected System.Web.UI.WebControls.DropDownList ddlTimeZone;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
				BindTimeZone();
		}

		private void BindTimeZone()
		{
			ddlTimeZone.DataSource = WindowsTimeZone.TimeZones;
			ddlTimeZone.DataTextField = "DisplayName";
			ddlTimeZone.DataValueField = "Id";
			ddlTimeZone.DataBind();

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnRegister_Click(object sender, System.EventArgs e)
		{
			if( DoRegister() )
				Response.Redirect("Cashbook.aspx");
		}

		private bool DoRegister()
		{
			bool result = false;
			string userName = this.txtUserName.Text.Trim();
			string pwd = this.txtPwd.Text.Trim();
			if( userName == string.Empty )
			{
				FlashMessage.SetFlash( "User name can't be empty!", FlashMessage.FlashMessageType.Error );
				return false;
			}

			if(  pwd == string.Empty )
			{
				FlashMessage.SetFlash( "Password can't be empty!", FlashMessage.FlashMessageType.Error );
				return false;
			}

			if( this.txtPwd.Text != this.txtConfirmPwd.Text )
			{
				FlashMessage.SetFlash( "Passwords do not match!", FlashMessage.FlashMessageType.Error );
				return false;
			}

			string md5pwd = MD5Helper.GetMd5Sum( pwd );
			try
			{
				int userId = BLSecurity.AddUser( userName, userName, md5pwd );
	
				if( userId != Consts.NO_ID )
				{
					//add a default classification & category
					if( LoginManager.Login( userName, md5pwd ) )
						result = true;

					int classificationId = BLClassification.AddClassification( "Default");
					if( classificationId != Consts.NO_ID)
						BLCategory.AddCategory( "Default", classificationId );
				}
				else
					FlashMessage.SetFlash( "Register faild!", FlashMessage.FlashMessageType.Error );

			}
			catch(Exception ex)
			{
				FlashMessage.SetFlash( ex.Message, FlashMessage.FlashMessageType.Error );
			}

			return result;

		}
	}
}
